/* Original code (c) Acorn Computers Ltd, 1992-3 */

/* $Id: c._Open 3.1 93/03/09 23:25:44 brian Exp $ */
#include "FS.h"

#define BUFFER_SIZE 1024

_kernel_oserror *fsentry_open( FSEntry_Open_Parameter *parm )
{
  _kernel_oserror *err;
  FileEntry *fse=(FileEntry*)parm->open_definition.handle;
  WHICH how;
#ifdef DEBUG
  printf("Open(%d)\n",parm->open_definition.reason);
#endif
  special_field = parm->open_definition.special_field;
  switch ( parm->open_definition.reason )
  {
  case FSEntry_Open_Reason_Update:      how = OPENUP;   break;
  case FSEntry_Open_Reason_OpenRead:    how = OPENIN;   break;
  case FSEntry_Open_Reason_CreateUpdate:how = CREATE;   break;
  default:
          return ERR(mb_BadParameters);
  }
    err = FileEntry_Open( NULL, parm->open_definition.filename, how, &fse );
    if ( err )
    { parm->open_result.handle = NULL;
      if ( err!=ERR(mb_FileNotFound) )
        return err;
    }
    else
    { FileDesc d = FileEntry_Desc( fse );
      int i;
      parm->open_result.handle = (FileSystemHandle)fse;
      i=0;
      if (d.attr&Attr_W && parm->open_definition.reason!=FSEntry_Open_Reason_OpenRead)
                         i|=InformationWord_WritePermitted;
      if (d.attr&Attr_R) i|=InformationWord_ReadPermitted;
      if (!d.buffered && !d.noosgbpb)
                         i|=InformationWord_UnbufferedOS_GBPBSupported;
      if (d.interactive) i|=InformationWord_StreamIsInteractive;
      if (2==d.type)     i|=InformationWord_ObjectIsDirectory;
      parm->open_result.information_word=i;
      if ( d.buffered )
      { parm->open_result.buffer_size=BUFFER_SIZE;
        parm->open_result.file_extent=d.length;
        parm->open_result.allocated_space=FileEntry_Allocated(fse);
      }
      else
        parm->open_result.buffer_size=0;
    }
    return NULL;

}

